<template>
  <div class="product-receiving-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>产品收料</span>
          <el-button type="primary" @click="handleCreate">新建产品收料</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="工单号">
          <el-input v-model="queryForm.workOrderNo" placeholder="请输入工单号" />
        </el-form-item>
        <el-form-item label="产品编码">
          <el-input v-model="queryForm.productCode" placeholder="请输入产品编码" />
        </el-form-item>
        <el-form-item label="收料日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="待收料" value="pending" />
            <el-option label="已完成" value="completed" />
            <el-option label="已取消" value="cancelled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="workOrderNo" label="工单号" width="180" />
        <el-table-column prop="productCode" label="产品编码" width="150" />
        <el-table-column prop="productName" label="产品名称" />
        <el-table-column prop="planQuantity" label="计划数量" width="100" />
        <el-table-column prop="receivedQuantity" label="已收数量" width="100" />
        <el-table-column prop="receiveDate" label="收料日期" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button 
              link 
              type="primary" 
              @click="handleReceive(scope.row)"
              v-if="scope.row.status === 'pending'"
            >收料</el-button>
            <el-button 
              link 
              type="danger" 
              @click="handleCancel(scope.row)"
              v-if="scope.row.status === 'pending'"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 收料对话框 -->
    <el-dialog
      v-model="receiveDialogVisible"
      title="产品收料"
      width="600px"
    >
      <el-form :model="receiveForm" label-width="100px" :rules="rules" ref="receiveFormRef">
        <el-form-item label="工单号">
          <el-input v-model="receiveForm.workOrderNo" disabled />
        </el-form-item>
        <el-form-item label="产品信息">
          <el-input v-model="receiveForm.productInfo" disabled />
        </el-form-item>
        <el-form-item label="收料仓库" prop="warehouse">
          <el-select v-model="receiveForm.warehouse" placeholder="请选择仓库">
            <el-option
              v-for="item in warehouseOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收料数量" prop="quantity">
          <el-input-number 
            v-model="receiveForm.quantity" 
            :min="1" 
            :max="receiveForm.maxQuantity"
          />
          <span class="quantity-info">
            可收料数量：{{ receiveForm.maxQuantity }}
          </span>
        </el-form-item>
        <el-form-item label="质检状态" prop="qcStatus">
          <el-select v-model="receiveForm.qcStatus">
            <el-option label="合格" value="qualified" />
            <el-option label="待检" value="pending" />
            <el-option label="不合格" value="unqualified" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="receiveForm.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="receiveDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleReceiveSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// ... 省略部分代码 ...

const rules = {
  warehouse: [{ required: true, message: '请选择收料仓库', trigger: 'change' }],
  quantity: [{ required: true, message: '请输入收料数量', trigger: 'blur' }],
  qcStatus: [{ required: true, message: '请选择质检状态', trigger: 'change' }]
}

const handleReceiveSubmit = async () => {
  if (!receiveFormRef.value) return
  
  try {
    await receiveFormRef.value.validate()
    // 实现收料逻辑
    ElMessage.success('收料成功')
    receiveDialogVisible.value = false
  } catch (error) {
    console.log('表单验证失败', error)
  }
}
</script>

<style scoped>
.product-receiving-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.quantity-info {
  margin-left: 10px;
  color: #909399;
}
</style> 